package com.atguigu.edu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.edu.dto.TeacherQueryDTO;
import com.atguigu.edu.entity.Teacher;
import com.atguigu.edu.service.TeacherService;
import com.atguigu.serevicebase.exception.GuliException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zhangsj
 * @since 2020-07-01
 */
@Api(value = "API - TeacherController", description = "讲师管理")
@RestController
@RequestMapping("/edu/teacher")
@CrossOrigin
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R list() {

        try {
            int a = 10 / 0;
        } catch (Exception e) {
            throw new GuliException(20002, "算数异常");
        }

        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }


    @ApiOperation(value = "分页讲师列表")
    @PostMapping("/{page}/{limit}")
    public R pageQuery(@ApiParam(name = "page", value = "当前页码", required = true)
                       @PathVariable Long page,
                       @ApiParam(name = "limit", value = "每页纪录数", required = true)
                       @PathVariable Long limit,
                       @ApiParam(name = "teacherQueryDTO", value = "查询对象", required = false)
                       @RequestBody TeacherQueryDTO queryDTO) {
        Page<Teacher> pageParam = new Page<>(page, limit);
        teacherService.pageQuery(pageParam, queryDTO);

        return R.ok().data("rows", pageParam.getRecords()).data("total", pageParam.getTotal());
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/{id}")
    public R removeById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        boolean b = teacherService.removeById(id);
        return R.ok().data("result", b);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("/save")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher) {
        teacherService.save(teacher);

        return R.ok();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("/{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师id", required = true)
            @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);

        return R.ok().data("teacher", teacher);
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
                    Teacher teacher) {
        teacher.setId(id);

        teacherService.updateById(teacher);

        return R.ok();
    }
}

